package com.lswms.modules.lswms.service.impl.uniquecode;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.common.utils.Result;
import com.lswms.modules.lswms.dao.uniquecode.UniquecodeDao;
import com.lswms.modules.lswms.dao.uniquecode.UniquecoderecordDao;
import com.lswms.modules.lswms.dto.uniquecode.UniquecodeDTO;
import com.lswms.modules.lswms.dto.uniquecode.UniquecoderecordDTO;
import com.lswms.modules.lswms.entity.orders.OrderdetaillistEntity;
import com.lswms.modules.lswms.entity.stock.InventoryEntity;
import com.lswms.modules.lswms.entity.uniquecode.UniquecodeEntity;
import com.lswms.modules.lswms.entity.uniquecode.UniquecoderecordEntity;
import com.lswms.modules.lswms.service.uniquecode.UniquecodeService;

import springfox.documentation.annotations.ApiIgnore;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 唯一码
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-07-15
 */
@Service
public class UniquecodeServiceImpl extends CrudServiceImpl<UniquecodeDao, UniquecodeEntity, UniquecodeDTO> implements UniquecodeService {
    @Autowired
    private UniquecodeDao uniquecodeDao;

    @Autowired
    private UniquecoderecordServiceImpl uniquecoderecordService;

    @Autowired
    private UniquecoderecordDao uniquecoderecordDao;

    @Autowired
    private UniquecodeServiceImpl uniquecodeService;

    @Override
    public QueryWrapper<UniquecodeEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String unique = (String)params.get("unique");

        QueryWrapper<UniquecodeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(unique), "unique", unique);

        return wrapper;
    }

//    移箱
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result transferTrayUniquecod(List<InventoryEntity> list){
        UniquecoderecordEntity uniquecoderecordEntity=new UniquecoderecordEntity();
        for (InventoryEntity inventoryEntity : list) {
            String location = inventoryEntity.getLocation();
            String tray = inventoryEntity.getTray();
            String box = inventoryEntity.getBox();
            String lot = inventoryEntity.getLot();
            String sku = inventoryEntity.getSku();
            BigDecimal qty = inventoryEntity.getQty();
            BigDecimal pickqty = inventoryEntity.getPickqty();
            BigDecimal putqty = inventoryEntity.getPutqty();
            String totray = inventoryEntity.getTotray();
            String tobox = inventoryEntity.getTobox();
            BigDecimal toqty = inventoryEntity.getToqty();
            String isuniquecode = inventoryEntity.getIsuniquecode();
            String uniquecodes = inventoryEntity.getUniquecodes();
            if (isuniquecode.equals("0")){
                continue;
            }
            String[] split = uniquecodes.split("\n");
            for (String item : split) {
                Integer integer = uniquecodeDao.checkUniquecode(item);
                if (integer!=1){
                    return new Result().error("部分唯一码不存在");
                }
            }
            for (String item : split) {
                UniquecodeEntity entity = uniquecodeDao.getListByUniquecode(item);
                String checkTray = entity.getTray();
                String checkBox = entity.getBox();
                if(!(checkBox.equals(box)&&checkTray.equals(tray))){
                    return new Result().error("该箱位没有对应的唯一码");
                }
            }
            for (String item : split) {
                UniquecodeEntity entity = uniquecodeDao.getListByUniquecode(item);
                BeanUtils.copyProperties(entity,uniquecoderecordEntity);
                uniquecoderecordEntity.setFromtray(entity.getTray());
                uniquecoderecordEntity.setTotray(totray);
              
                uniquecoderecordEntity.setFrombox(entity.getBox());
                uniquecoderecordEntity.setTobox(tobox);
                entity.setTray(totray);
                entity.setBox(tobox);
                uniquecodeDao.updateById(entity);
                uniquecoderecordService.insert(uniquecoderecordEntity);
            }

        }

        return new Result();
    }

    @Override
    public  void getInventoryList(List<InventoryEntity> list){

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result pickListUniquecode(List<OrderdetaillistEntity> list){
        UniquecoderecordEntity uniquecoderecordEntity=new UniquecoderecordEntity();
        for (OrderdetaillistEntity entity :list) {
            String fromtray = entity.getFromtray();
            String frombox = entity.getFrombox();
            String totray = entity.getTotray();
            String tobox = entity.getTobox();
            String uniquecodes = entity.getUniquecodes();
            String isuniquecode = entity.getIsuniquecode();
            if (isuniquecode.equals("0")){
                continue;
            }
            String[] split = uniquecodes.split("\n");
            for (String item : split) {
                Integer integer = uniquecodeDao.checkUniquecode(item);
                if (integer!=1){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result().error("部分唯一码不存在");
                }
            }
            for (String item : split) {
                UniquecodeEntity uniquecodeEntity = uniquecodeDao.getListByUniquecode(item);
                String checkTray = uniquecodeEntity.getTray();
                String checkBox = uniquecodeEntity.getBox();
                if(!(checkBox.equals(frombox)&&checkTray.equals(fromtray))){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result().error("该箱位没有对应的唯一码");
                }
            }
            for (String item : split) {
                UniquecodeEntity uniquecodeET= uniquecodeDao.getListByUniquecode(item);
                BeanUtils.copyProperties(uniquecodeET,uniquecoderecordEntity);
                uniquecoderecordEntity.setFromtray(uniquecodeET.getTray());
                uniquecoderecordEntity.setTotray(totray);
//                uniquecoderecordEntity.setTotraytime(new Date());
                uniquecoderecordEntity.setFrombox(uniquecodeET.getBox());
                uniquecoderecordEntity.setTobox(tobox);
//                uniquecoderecordEntity.setToboxtime(new Date());
                uniquecodeET.setTray(totray);
                uniquecodeET.setBox(tobox);
                uniquecodeDao.updateById(uniquecodeET);
                uniquecoderecordService.insert(uniquecoderecordEntity);
            }
        }
        return new Result();
    }

    @Transactional(rollbackFor = Exception.class)
    public Result deliveryUniquecode(List<OrderdetaillistEntity> list){

        for (OrderdetaillistEntity orderdetaillistEntity : list){
            UniquecoderecordDTO uniquecoderecordDTO=new UniquecoderecordDTO();
            QueryWrapper<UniquecodeEntity> uniquecodeQuery = new QueryWrapper<UniquecodeEntity>();
            uniquecodeQuery.eq("tray",orderdetaillistEntity.getTray());
            uniquecodeQuery.eq("box",orderdetaillistEntity.getBox());
            List<UniquecodeEntity> uniquecodeEntities = uniquecodeDao.selectList(uniquecodeQuery);
            QueryWrapper<UniquecoderecordEntity> UniquecoderecordQuery = new QueryWrapper<UniquecoderecordEntity>();
            for (UniquecodeEntity entity : uniquecodeEntities) {
                UniquecoderecordQuery.eq("uniqueCode", entity.getUniquecode());
                uniquecodeDao.deleteById(entity.getId());
            }

//            List<UniquecoderecordEntity> uniquecoderecordEntities = uniquecoderecordDao.selectList(UniquecoderecordQuery);
//            for (UniquecoderecordEntity uniquecoderecordEntity:uniquecoderecordEntities) {
//                uniquecoderecordEntity.setOrderkey(orderdetaillistEntity.getOrderkey());
//                uniquecoderecordEntity.setOrdertime(new Date());
//                BeanUtils.copyProperties(uniquecoderecordEntity,uniquecoderecordDTO);
//                uniquecoderecordService.save(uniquecoderecordDTO);
//            }
        }
        return new Result();
    }

	@Override
	public List<UniquecodeEntity> getByTray(Map<String, Object> params) {
		  String lot = (String)params.get("lot");
	        String tray = (String)params.get("tray");
	        String box = (String)params.get("box");
	        String sku = (String)params.get("sku");
		
		QueryWrapper<UniquecodeEntity> query = new QueryWrapper<UniquecodeEntity>();
		query.eq("lot", lot);
		query.eq("tray", tray);
		query.eq("box", box);
		query.eq("sku", sku);
		
		List<UniquecodeEntity> list = baseDao.selectList(query);
		return list;
	}
}
